//
//  EeKitsView.swift
//  soapp_Example
//
//  Created by zhu xietong on 2018/2/13.
//  Copyright © 2018年 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import AsyncDisplayKit
import Eelay

//
//extension String
//{
//    fileprivate func float(sep:String,index:Int)->CGFloat
//    {
//        let paths = self.components(separatedBy:sep)
//        return paths[index].cg_floatValue
//    }
//}
open class JoItemsNode<Kit:JoTouchNode>:MoDisplayNode,EeActionDelegate{
    public func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
        active(ID: actionID, object: data)
    }
    
    
    
    public var nodes:[Kit] = [Kit]()
    
    public var true_height:CGFloat?
    public var layoutInfo:[String:Any]?
    {
        didSet{
            self.buildView()
        }
    }
    public var view_width = UIScreen.main.bounds.size.width
    
    public var space:(x:CGFloat,y:CGFloat) = (TP.separatorHeight,TP.separatorHeight)
    
    public var singleLine = false
    
    public var layouts = NSMutableDictionary()
    
    
    public var padding = UIEdgeInsets.zero
    
    public var list:NSMutableArray = NSMutableArray()
    {
        didSet{
            self.loadItems()
        }
    }
    
    
    
    public func buildView()
    {
        
        //        for one in self.subviews
        //        {
        //            one.removeFromSuperview()
        //        }
        
        if let info = self.layoutInfo
        {
            self.layouts = info.mutable_dictionary
            
            self.layoutViews()
            
        }
    }
    
    
    public func loadItems()
    {
        var index = 0
        for one in nodes
        {
            one.mo.model = DataNode.path(node: "\(index)", list, value: NSMutableDictionary())
//            one.controller = controller
            index = index + 1
        }
        
    }
    
    
    func layoutViews()
    {
        
        let kit_frame = self.layouts["frame","1*1"]
        let frame_info = kit_frame.components(separatedBy:"*")
        let frame_w_c = frame_info[0].cg_floatValue
        let frame_h_c = frame_info[1].cg_floatValue
        
        var edigStr = self.layouts["edige",""]
        if edigStr.len < 1{
            edigStr = layouts["padding","0|0|0|0"]
        }
        
        view_width = layouts[float:"width",UIScreen.main.bounds.size.width];
        
        let scale = self.layouts[float:"size_scale",0.0]
        
        let space = layouts ["space","0|0"]
        
        let spaceX = space.float(sep: "|", index: 0)
        let spaceY = space.float(sep: "|", index: 1)
        
        self.space = (spaceX,spaceY)
        
        var view_height = scale*view_width
        
        let h = layouts[float:"height",0.0]
        if h > 0
        {
            view_height = h
        }
        
        
        
        let items = self.layouts[obj:"kits",nil] as! NSMutableArray
        self.list = items
        
        
        let eT = edigStr.float(sep: "|", index: 0)
        let eL = edigStr.float(sep: "|", index: 1)
        let eB = edigStr.float(sep: "|", index: 2)
        let eR = edigStr.float(sep: "|", index: 3)
        
        
        let all_width = view_width + spaceX
        let all_heigth = view_height + spaceY
        
        
        let one_width = (all_width - eL - eR) / frame_w_c
        let one_height = (all_heigth - eT - eB) / frame_h_c
        
        
        padding = UIEdgeInsets(top: eT, left: eL, bottom: eB, right: eR)
        
        var index = 0
        
        
        
        for dict in items
        {
            
            
            if let one = dict as? NSMutableDictionary
            {
                
                let f_str = one ["frame","0|0|0|0"]
                let originX = eL + (f_str.float(sep: "|", index: 0) * one_width)
                let originY = eT + (f_str.float(sep: "|", index: 1) * one_height)
                
                let w = one_width * f_str.float(sep: "|", index: 2) - spaceX
                let h = one_height * f_str.float(sep: "|", index: 3) - spaceY
                
                
                let one_kit = Kit()
                
                
                let _rect:CGRect = CGRect(x: originX, y: originY, width: w, height: h)
                
                
                one_kit.delegate = self
                nodes.append(one_kit)
                one_kit.style.layoutPosition = _rect.origin
                one_kit.style.preferredSize = _rect.size
                
                one.setObject("\(index)", forKey: "__index" as NSCopying)
                //                addSubnode(one_kit)
                //                one_kit.mo.model = one
                index += 1
            }
        }
        self.backgroundColor = UIColor(shex:layouts["back_hex","#fff"])
        self.style.width = ASDimension(unit: .points, value: view_width)
        self.style.height = ASDimension(unit: .points, value: view_height)
        //        self.setNeedsLayout()
        //        self.supernode?.setNeedsLayout()
                
    }
    
    
    
    override open func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
        return ASAbsoluteLayoutSpec(children: nodes)
    }
    
    
}




